<?php

namespace app\admin\controller\ygame;

use app\common\controller\Backend;
use EasyWeChat\Factory;
use QRcode\QRcode;
use think\Request;

/**
 * 赛事管理
 *
 * @icon fa fa-circle-o
 */
class Project extends Backend
{
    
    /**
     * Project模型对象
     * @var \app\admin\model\ygame\Project
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ygame\Project;

    }

    //查看小程序码
    public function seexcx(){
        $ids = $this->request->param("ids");

        $Model = new \app\admin\model\ygame\Project();
        $data = $Model->where(['id'=>$ids])->find();

        $config = get_addon_config('ygame');
        $options = [
            'app_id'   => $config['mini']['app_id'],
            'secret'   => $config['mini']['app_secret'],
        ];
        $app = Factory::miniProgram($options);
        $response = $app->app_code->get("pages/sign/detail?project_id=".$ids,['width'=>180]);

        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $response->saveAs("./uploads/".date("Ymd"), md5($data['id']).".png");
            $qr_path = "./uploads/".date("Ymd")."/".md5($data['id']).".png";
        }

        $this->assign('qrpath',Request::instance()->domain()."/".$qr_path);
        return $this->fetch();
    }
}
